<?php
include_once('models/catenew_model.php');
include_once('controllers/backend_controller.php');

class Catenews_controller extends Backend_Controller {
    public $model;
    public $action = 'lists';

    public function __construct(){
        parent::__construct();
        $this->model = new Catenews_Model();
        if(isset($_GET['action'])) $this->action = $_GET['action'];
        $this->{$this->action}();
    }
    public function lists() {
        $listCategories = $this->model->getListCate();
        include 'views/categories/catnews-list.php';
    }
    public function add() {
        if(isset($_POST["btn_submit"])) {
            if(($_POST['nameCat'] != '') && ($_POST['descr'] != '')){
                $nameCat = $_POST['nameCat'];
                $description = $_POST['descr'];
                $addCat = $this->model->addCat($nameCat,$description);
                header ('Location: index.php?controller=catenews&action=lists');
            }
            else
            {
                include 'views/categories/addcatNews.php';
            }
        }
        else
        {
            include 'views/categories/addcatNews.php';
        }
    }
    public function delete(){
        $id=$_GET['id'];
        $delCat = $this->model->delCat($id);
        header ('Location: index.php?controller=catenews&action=lists');
    }
    public  function edit(){
        if(isset($_POST['btn_submit'])){
            if(isset($_POST['nameCat'])&& isset($_POST['descr'])){
                $name =$_POST['nameCat'];
                $descr=$_POST['descr'];
                $id = $_POST['id'];
                $this->model->editCat($name,$descr,$id);
                header ('Location: index.php?controller=catenews&action=lists');
            }

        }
        else{
            $id=$_GET['id'];
            $beforeditCat = $this->model->beforeditCat($id);
            include 'views/categories/editCatNews.php';
        }
    }
    public function view(){
        $id = $_GET['id'];
        $viewProductCat =  $this->model->viewCat($id);
        include 'views/categories/viewNewsCat.php';
    }
    public  function listAllProduct(){
        $id=$_GET['id'];
        $artPro = $this->model->getListProductforEachcat($id);
        include 'views/new/news-lists.php';
    }
}
?>